package controller;

import java.util.ArrayList;
import java.util.NoSuchElementException;

import manager.CompletedCourseMgr;
import manager.StudentMgr;
import entity.Course;
import entity.Student;

/**
 * @author sheryl.tay.2012, mohameds.2012
 * 
 */
public class StudentCtrl {

	private CompletedCourseMgr completedCourseMgr;
	private StudentMgr studentMgr;

	public StudentCtrl() {
		studentMgr = new StudentMgr();
		completedCourseMgr = new CompletedCourseMgr();
	}

	public void addCompletedCourseForStudent(String courseCode, String userId) {
		completedCourseMgr.addCompletedCourseForStudent(courseCode, userId);
	}

	public void addStudent(Student student) {
		studentMgr.addStudent(student);
	}

	public boolean authenticateStudent(String userId, String password) {
		try {
			Student student = studentMgr.retrieveStudentForUserId(userId);
			if (student.getPassword().equals(password)) {
				return true;
			}
		} catch (NoSuchElementException e) {
			// user doesn't exist in our database
		}

		return false;
	}

	public void deductEdollarForStudent(double amountToDeduct, Student student) {
		double newAmount = student.getEdollar() - amountToDeduct;
		studentMgr.updateEdollarForStudent(newAmount, student.getUserId());
	}

	public ArrayList<Course> retrieveCompletedCoursesForStudent(String userId) {
		return completedCourseMgr.retrieveCompletedCoursesForStudent(userId);
	}

	public void updateEdollarForStudent(double newAmount, String userId) {
		studentMgr.updateEdollarForStudent(newAmount, userId);
	}

	public Student retrieveStudentForUserId(String userId) {
		return studentMgr.retrieveStudentForUserId(userId);
	}

}
